auto.waitFor();

var now = new Date();
var min = now.getMinutes();
var hour = now.getHours();
var date = now.getDate();
var month = now.getMonth();
var storage = storages.create(month + '-' + date);
if (hour < 10 && storage.contains('up')) {
    exit();
}
var myFunc = require("myCommonFunction.js");
myFunc.initNetWork(null, false);

opreate();

function opreate() {
    try {
        if (hour < 11 && !storage.contains('up')) {
            上班打卡();
        } else if (hour > 17) {
            下班();
        }
    } catch (error) {
        log(error);
        myFunc.tuichu('阿里钉');
        opreate();
    }
}



function 上班打卡() {
    log('上班打卡');
    myFunc.openMyApp("阿里钉");

    if (myFunc.textpClick('迟到打卡')) {
        log('迟到打卡');
        now = new Date();
        min = now.getMinutes();
        hour = now.getHours();
        storage.put('up', hour + "-" + min);
        log('iPSA 打卡');
        myFunc.openMyApp("iPSA");
        myFunc.clickByText("取消", 6000);
        myFunc.clickByText('业务');
        myFunc.textpClick('考勤打卡');
        sleep(8000);
        log('签到')
        myFunc.textpClick('签到', 5000);
        return;
    }

    myFunc.textpClick('打卡', 6000);

    textContains('上班').findOne(5000);
    if (text('无法打卡').findOnce() != null) {
        log('无法打卡');
        //等两分钟再打卡
        setTimeout(function () {
            log('等两分钟打卡');
            opreate();
        }, 120000);
    } else if (text('上班打卡').findOne(1500) != null) {
        log('上班打卡');
        now = new Date();
        min = now.getMinutes();
        hour = now.getHours();
        sleep(2000);
        myFunc.textpClick('上班打卡');
        if (min > 30) {
            myFunc.textpClick('迟到打卡');
        }
        log('打卡', hour, min);
        storage.put('up', hour + "-" + min);


        myFunc.openMyApp("iPSA");
        myFunc.clickByText("取消", 6000);
        myFunc.clickByText('业务');
        myFunc.textpClick('新考勤打卡');
        sleep(8000);
        log('签到')
        myFunc.textpClick('签到', 5000);
    } else if (textContains('已打卡').findOnce() != null && text('下班打卡').findOnce() != null) {
        log('上班已打卡');
        var shangbanTime = textContains('已打卡').findOnce().text();
        var time = shangbanTime.split('已')[0].replace(':', '-');
        storage.put('up', time);
    }


}



function 下班() {
    now = new Date();
    min = now.getMinutes();
    hour = now.getHours();
    //判断上下班时长是否大于9
    //打下班卡, 7点直接打，6点根据上班时间打 上班时间+9h 1min
    if (hour > 18) {
        log('18点后打卡');
        下班打卡();
    } else {
        //判断上下班时长是否大于9
        var upTime = storage.get('up');
        if (upTime == null) {
            var shangbanTime = textContains('已打卡').findOnce().text();
            upTime = shangbanTime.split('已')[0].split(':');
        } else {
            upTime = upTime.split('-');
        }
        var xiaHour = parseInt(upTime[0]) + 9;
        //小于9等到大于9小时再打卡
        if (xiaHour == hour && upTime[1] >= min) {
            var waitTime = min - upTime[1] + 1;
            log('等', waitTime);
            setTimeout(function () {
                下班();
            }, waitTime * 60000);
        } else {
            log('18点大于9小时打卡');
            //大于9小时
            下班打卡();
        }
    }
}
function 下班打卡() {
    myFunc.openMyApp("阿里钉");

    myFunc.textpClick('打卡', 6000);

    textContains('上班').findOne(5000);

    sleep(2000);

    //判断是否已打卡，打卡or更新打卡
    if (text('未打卡').findOnce() != null && text('下班打卡').findOnce() != null) {
        myFunc.textpClick('下班打卡');
    } else if (textContains('已打卡').find().length == 2) {
        //判断是否需要更新打卡
        var ttime = textContains('已打卡').find();
        var startTime = ttime[0].text().split('已')[0].split(':');
        var endTime = ttime[1].text().split('已')[0].split(':');
        if (endTime[0] < 18 || (18 == endTime[0] && startTime[1] > endTime[1])) {
            text('更新打卡').findOnce().click();
            text('确定').findOne(3000).click();
        }
    }
    myFunc.openMyApp("iPSA");
    myFunc.clickByText('业务');
    myFunc.textpClick('新考勤打卡');
    sleep(8000);
    myFunc.textpClick('签退', 5000);
}
